package com.iyundao.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iyundao.io.Service.SysDeptService;
import com.iyundao.io.model.SysDept;
import com.iyundao.io.model.SysDeptProvider;
import com.iyundao.io.model.SysUserProvider;
import com.iyundao.io.tools.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by black on 2019/4/27.
 */
@RestController
@RequestMapping(value = "/api/dept")
public class DeptController {

    @Autowired
    SysDeptService sysDeptService;

    @ApiOperation(value="获取全部组织树", notes="获取全部组织树")
    @RequestMapping(value = "/findAllTree", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData findAllTree(){
        return new ResponseData(200,"获取成功",sysDeptService.getAllTree());
    }

    @ApiOperation(value="获取全部组织树", notes="获取全部组织树")
    @RequestMapping(value = "/findParentTree", method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseData findParentTree(@RequestParam String params){
        return new ResponseData(200,"获取成功",sysDeptService.getTreeByParentId(params));
    }

    @ApiOperation(value="根据id查询本组织以及以下组织", notes="根据id查询本组织以及以下组织")
    @RequestMapping(value = "/selectbysid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectBySid(){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getSysDept().getSid();
        if (id == "1" || id.equals("1")){
            return new ResponseData(200,"获取成功",sysDeptService.getAllTree());
        } else {
            SysDeptProvider sysDeptProvider = sysDeptService.selectByRoleId(id);
            List<SysDeptProvider> list = new ArrayList<>();
            list.add(sysDeptProvider);
            return new ResponseData(200,"获取成功",list);
        }

    }

    @ApiOperation(value="用户新增", notes="用户新增")
    @RequestMapping(value = "/newDeptInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData newDeptInfo(@RequestBody JSONObject params){
        SysDept sysDept = JSON.toJavaObject(params,SysDept.class);
        sysDept.setSid(UUID.randomUUID().toString());
        sysDept.setFlag("1");
        sysDept.setCreateDate(new Date());
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        sysDept.setCreateId(sysUser.getSid());
        sysDept.setOrdeBy((byte) 1);
        return new ResponseData(200,"添加成功", sysDeptService.addNewTree(sysDept));
    }

    @ApiOperation(value="用户修改", notes="用户修改")
    @RequestMapping(value = "/updateDeptInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData updateDeptInfo(@RequestBody JSONObject params){
        SysDept sysDept = JSON.toJavaObject(params,SysDept.class);
        sysDept.setFlag("1");
        return new ResponseData(200,"添加成功", sysDeptService.updateDept(sysDept));
    }
}
